<template>
  <GlobalContainer className="flex flex-col !bg-[#f3f3f3]" :backgroundImage="`${OSS_URL}/images/baoxiu/head_bg.png`">
    <GlobalHeader ref="headerRef" title="订单列表" showBack :backgroundImage="`${OSS_URL}/images/baoxiu/head_bg.png`" :fixed="true" />
    <div class="px-24">
      <div class="status-bar-container fixed" :style="{ top: `${headerHeight}` }">
        <div
          class="status-bar-item"
          v-for="(item, index) in statusBarList"
          :key="index"
          :class="statusBarActive === item.value && 'status-bar-item-active'"
          @tap="statusBarChange(item.value)"
        >
          {{ item.name }}
        </div>
      </div>

      <div class="list-c rounded-20" v-if="list.length > 0" :style="{ marginTop: '100rpx' }">
        <div class="item-c py-30 mb-20 pl-32 pr-44" v-for="item in list" :key="item.id" @tap="toDetailHandle(item)">
          <div class="line1 mb-22">
            <div class="mr-22 h-28 w-8 bg-[#1e90ff]" />
            <div class="text-32 leading-45 text-[#303133]">{{ item.productDesc }}</div>
            <div class="status ml-auto" :class="getStatusClass(item.status) || 'status-default'">{{ getStatusText(item.status) }}</div>
            <image class="ml-20 size-32" src="../../static/images/jiaofei_right_icon.png" />
          </div>
          <div class="line2">
            <span class="">{{ item.address }}</span>
          </div>
          <div class="line3">
            <span class="text-30">{{ item.createdTime }}</span>
          </div>
          <div class="line4 mb-20">
            <span class="">{{ item.contacts }} {{ item.phone }}</span>
          </div>
          <div class="text-26 leading-60 mt-20 flex h-60 text-center" v-if="item.status === 10">
            <div
              class="w-160 rounded-40 ml-auto mr-20 border-2 border-[#CDD0D6] !bg-white !text-[#606266]"
              @tap.stop="cancelHandle(item.id)"
            >
              取消预约
            </div>
            <div class="w-160 rounded-40 bg-[#1E90FF] text-white" @tap.stop="payOrder(item.orderNo)">立即支付</div>
          </div>
          <div class="text-26 leading-60 mt-20 flex h-60 text-center" v-if="item.status === 20">
            <div class="w-160 rounded-40 ml-auto bg-[#1E90FF] text-white" @tap.stop="refundHandle(item.id)">退款</div>
          </div>
        </div>
      </div>
    </div>

    <div v-if="list.length === 0" class="empty-container mt-240 mx-auto flex flex-col items-center">
      <div class="empty-img mb-40">
        <image class="h-240 w-330" src="../../static/images/jiaofei_empty.png" />
      </div>
      <div class="empty-text text-28 leading-36 text-[#909399]">暂无记录</div>
    </div>
    <div v-else class="empty-container mt-18 text-28 leading-36 text-center text-[#909399]">已加载全部记录</div>
  </GlobalContainer>
</template>

<script setup>
import { ref, computed } from 'vue'
import { showLoading, hideLoading, showModal } from '@/utils'
import { getOrderPage, cancelOrder, applyRefund, mallPayment } from '@/common/api/report'
import { useAccountStore } from '@/store'
const accountStore = useAccountStore()
const currentRoom = computed(() => accountStore.currentRoom)
const OSS_URL = import.meta.env.VITE_OSS_URL

const headerRef = ref()
const headerHeight = computed(() => {
  return headerRef.value?.wrapStyle?.height
})
const statusBarList = ref([
  {
    name: '全部',
    value: ''
  },
  {
    name: '待支付',
    value: 10
  },
  {
    name: '待服务',
    value: 20
  },
  {
    name: '已完成',
    value: 40
  },
  {
    name: '已取消',
    value: 60
  }
])
const statusBarActive = ref('')

const list = ref([])

const statusBarChange = active => {
  statusBarActive.value = active
  getPageHandle(active)
}
const getPageHandle = async (status = '') => {
  showLoading()
  getOrderPage({ pageNo: 1, pageSize: 9999, status })
    .then(res => {
      list.value = res.records || []
    })
    .finally(() => {
      hideLoading()
      uni.stopPullDownRefresh()
    })
}

const getStatusClass = status => {
  switch (status) {
    case 10:
      return 'status-wait'
    case 20:
      return 'status-ok'
    case 40:
      return 'status-ok'
    case 60:
      return 'status-not'
    default:
      return ''
  }
}

const getStatusText = status => {
  switch (status) {
    case 0:
      return '已关闭'
    case 10:
      return '待支付'
    case 20:
      return '待服务'
    case 40:
      return '已完成'
    case 60:
      return '已取消'
    default:
      return ''
  }
}

const toDetailHandle = item => {
  uni.navigateTo({
    url: '/subPages/wuye/orderDetail?orderId=' + item.id
  })
  // Navigate to detail page with item data
}

const cancelHandle = id => {
  showModal({
    title: '提示',
    content: '确定取消预约吗？',
    confirmText: '确定',
    cancelText: '取消',
    success: () => {
      cancelOrder({ id }).then(res => {
        if (res) {
          getPageHandle(statusBarActive.value)
        }
      })
    },
    cancel: () => {}
  })
}
const refundHandle = id => {
  showModal({
    title: '提示',
    content: '确定申请退款吗？',
    confirmText: '确定',
    cancelText: '取消',
    success: () => {
      uni.showLoading({
        title: '处理中'
      })
      applyRefund({ id })
        .then(res => {
          getPageHandle(statusBarActive.value)
        })
        .finally(() => {
          uni.hideLoading()
        })
    },
    cancel: () => {}
  })
}

const payOrder = orderNo => {
  uni.showLoading({
    title: '支付中'
  })
  mallPayment({ mallOrderNo: orderNo }).then(res => {
    const { prepayId, nonceStr, timeStamp, paySign } = res
    uni.requestPayment({
      provider: 'wxpay',
      timeStamp: String(timeStamp),
      nonceStr: nonceStr,
      package: 'prepay_id=' + prepayId,
      signType: 'SHA256withRSA',
      paySign: paySign,
      orderInfo: {
        appId: '',
        timeStamp: String(timeStamp),
        nonceStr: nonceStr,
        package: ''
      },
      success: res => {
        console.log('[ res ] >', res)
        uni.switchTab({
          url: '/pages/home/index'
        })
      },
      complete: () => {
        uni.hideLoading()
        getPageHandle(statusBarActive.value)
      }
    })
  })
}

onShow(() => {
  getPageHandle()
})

onPullDownRefresh(() => {
  getPageHandle()
})
</script>

<style scoped lang="scss">
.status-bar-container {
  width: 702rpx;
  left: 50%;
  transform: translateX(-50%);
  height: 80rpx;
  line-height: 1;
  text-align: center;
  background: #ffffff;
  border-radius: 10rpx 10rpx 10rpx 10rpx;
  padding: 24rpx 0;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.status-bar-item {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background-color: #fff;
  font-weight: 400;
  font-size: 28rpx;
  color: #606266;
  border-right: 1px solid #e3e7ec;
}
.status-bar-item-active {
  color: #1e90ff;
}

.list-c {
  .item-c {
    background: #fcfeff;
    border-radius: 20rpx 20rpx 20rpx 20rpx;
    border: 1rpx solid #ffffff;
    .line1,
    .line2,
    .line3 {
      display: flex;
      align-items: center;
    }
    .line2,
    .line3,
    .line4 {
      padding-left: 30rpx;
      font-weight: 400;
      font-size: 28rpx;
      color: #303133;
      line-height: 42rpx;
      margin-bottom: 12rpx;
    }
  }
}
.status {
  width: 118rpx;
  height: 52rpx;
  text-align: center;
  line-height: 52rpx;
  font-weight: 400;
  font-size: 26rpx;
  color: #ffffff;
  border-radius: 12rpx 0rpx 12rpx 0rpx;
  box-shadow: inset 0rpx 8rpx 16rpx 0rpx rgba(255, 255, 255, 0.3);
}
.status-ok {
  background: linear-gradient(303deg, #a3b4cc 0%, #8fa1b2 100%);
}
.status-wait {
  background: linear-gradient(303deg, #ff9933 0%, #ff8000 100%);
}
.status-not {
  background: linear-gradient(303deg, #ff5533 0%, #ff3333 100%);
}
.status-default {
  background: linear-gradient(303deg, #a3b4cc 0%, #8fa1b2 100%);
}
</style>
